package com.dd.machine.base.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.util.UUID;

public class DeviceInfo {
	/*
	 * public static String getDEVICE_ID(Context ctx) { TelephonyManager
	 * telephonyManager = (TelephonyManager) ctx
	 * .getSystemService(Context.TELEPHONY_SERVICE); String deviceID =
	 * telephonyManager.getDeviceId(); if(null==deviceID)return
	 * getSerial_Number(); return deviceID; }
	 * 
	 * @SuppressLint("NewApi") private static String getSerial_Number() { String
	 * str = android.os.Build.SERIAL.trim(); return str; }
	 */
	public static String getDEVICE_ID(Context ctx) {
		final TelephonyManager tm = (TelephonyManager) ctx
				.getSystemService(Context.TELEPHONY_SERVICE);
		final String tmDevice, tmSerial, androidId;
		tmDevice = "" + tm.getDeviceId();
		tmSerial = "" + tm.getSimSerialNumber();
		androidId = ""
				+ android.provider.Settings.Secure.getString(
				ctx.getContentResolver(),
				android.provider.Settings.Secure.ANDROID_ID);
		UUID deviceUuid = new UUID(HashAlgorithms.FNVHash1(androidId),
				((long) HashAlgorithms.FNVHash1(tmDevice) << 32) |HashAlgorithms.FNVHash1(tmSerial));
		return deviceUuid.toString();
	}

	/**
	 * deviceID的组成为：渠道标志+识别符来源标志+hash后的终端识别符
	 *
	 * 渠道标志为： 1，andriod（a）
	 *
	 * 识别符来源标志： 1， wifi mac地址（wifi）； 2， IMEI（imei）； 3， 序列号（sn）； 4，android_id;5:
	 * id：随机码。若前面的都取不到时，则随机生成一个随机码，需要缓存。
	 *
	 * @param context
	 * @return
	 */
	private static String getDeviceId(Context context) {

		StringBuilder device_cache = new StringBuilder();
		StringBuilder deviceId = new StringBuilder();
		// 渠道标志
		try {
			// wifi mac地址
			WifiManager wifi = (WifiManager) context
					.getSystemService(Context.WIFI_SERVICE);
			WifiInfo info = wifi.getConnectionInfo();
			String wifiMac = info.getMacAddress();
			if (!TextUtils.isEmpty(wifiMac)) {
				device_cache.append(wifiMac);
			}

			// IMEI（imei）
			TelephonyManager tm = (TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);
			String imei = tm.getDeviceId();
			if (!TextUtils.isEmpty(imei)) {
				device_cache.append(imei);
			}

			// 序列号（sn）
			String sn = tm.getSimSerialNumber();
			if (!TextUtils.isEmpty(sn)) {
				device_cache.append(sn);
			}
			//android_id
			String androidId = ""
					+ android.provider.Settings.Secure.getString(
					context.getContentResolver(),
					android.provider.Settings.Secure.ANDROID_ID);
			if (!TextUtils.isEmpty(androidId)) {
				device_cache.append(androidId);
			}
			// 如果上面都没有， 则生成一个id：随机码uuid
			if(TextUtils.isEmpty(device_cache)){
				deviceId.append(getUUID());
			}else{
				deviceId.append(UUID.fromString(device_cache.toString()));
			}

		} catch (Exception e) {
			e.printStackTrace();
			deviceId.append(getUUID());
		}
		return deviceId.toString();
	}
	private static String getUUID() {
		UUID uuid = UUID.randomUUID();
		String str = uuid.toString();
		return str;
	}
	private static String getUUID(Context context) {
		SharedPreferences sp = context.getSharedPreferences("sysCacheMap", Context.MODE_PRIVATE);
		String uuid="";
		if (sp != null) {
			uuid = sp.getString("uuid", "");
		}
		if(TextUtils.isEmpty(uuid)){
			uuid=getDeviceId(context);
			Editor edt=sp.edit();
			edt.putString("uuid", uuid);
			edt.commit();
		}
		return uuid;
	}

}
